package com.zxp.snowy.test.service.impl;

import com.zxp.snowy.test.entity.Simple;
import com.zxp.snowy.test.mapper.SimpleMapper;
import com.zxp.snowy.test.service.SimpleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import com.zxp.snowy.test.enums.SimpleExceptionEnum;
import com.zxp.snowy.test.mapper.SimpleMapper;
import com.zxp.snowy.test.param.SimpleParam;
import com.zxp.snowy.test.service.SimpleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 测试表 服务实现类
 * </p>
 *
 * @author shollin
 * @since 2024-09-11
 */
@Service
public class SimpleServiceImpl extends ServiceImpl<SimpleMapper, Simple> implements SimpleService {

    @Override
    public PageResult<Simple> page(SimpleParam paramRequest) {
        QueryWrapper<Simple> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(paramRequest)) {
            // 根据id 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getId())) {
                queryWrapper.lambda().eq(Simple::getId, paramRequest.getId());
            }
            // 根据姓名 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getName())) {
                queryWrapper.lambda().eq(Simple::getName, paramRequest.getName());
            }
            // 根据年龄 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getAge())) {
                queryWrapper.lambda().eq(Simple::getAge, paramRequest.getAge());
            }
            // 根据删除标识1 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getDeleteFlag())) {
                queryWrapper.lambda().eq(Simple::getDeleteFlag, paramRequest.getDeleteFlag());
            }
            // 根据删除标识2 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getDeleted())) {
                queryWrapper.lambda().eq(Simple::getDeleted, paramRequest.getDeleted());
            }
            // 根据测试布尔类型 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getIsOk())) {
                queryWrapper.lambda().eq(Simple::getIsOk, paramRequest.getIsOk());
            }
            // 根据版本 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getVersion())) {
                queryWrapper.lambda().eq(Simple::getVersion, paramRequest.getVersion());
            }
            // 根据创建时间 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getCreateTime())) {
                queryWrapper.lambda().eq(Simple::getCreateTime, paramRequest.getCreateTime());
            }
            // 根据更新时间 查询
            if (ObjectUtil.isNotEmpty(paramRequest.getUpdateTime())) {
                queryWrapper.lambda().eq(Simple::getUpdateTime, paramRequest.getUpdateTime());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Simple> list(SimpleParam paramRequest) {
        return this.list();
    }

    @Override
    public void add(SimpleParam paramRequest) {
        Simple tmpSimple = new Simple();
        BeanUtil.copyProperties(paramRequest, tmpSimple);
        this.save(tmpSimple);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SimpleParam> paramList) {
        paramList.forEach(paramRequest -> {
            Simple dbSimple = this.querySimple(paramRequest);
                this.removeById(dbSimple.getId());
            });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SimpleParam paramRequest) {
        Simple dbSimple = this.querySimple(paramRequest);
        BeanUtil.copyProperties(paramRequest, dbSimple);
        this.updateById(dbSimple);
    }

    @Override
    public Simple detail(SimpleParam paramRequest) {
        return this.querySimple(paramRequest);
    }

    /**
     * 根据主键获取对象
     */
    private Simple querySimple(SimpleParam paramRequest) {
        Simple dbSimple = this.getById(paramRequest.getId());
        if (ObjectUtil.isNull(dbSimple)) {
            throw new ServiceException(SimpleExceptionEnum.NOT_EXIST);
        }
        return dbSimple;
    }

}
